#include "Fish.h"
#include "global.h"

Fish::Fish(FishInfo* info,class Aquarium* environment): m_Info(info), m_Environment(environment)
{
		m_Entity = g_SceneManager->createEntity( m_Info->m_ModelSrc );

		m_Node = g_SceneManager->getRootSceneNode()->createChildSceneNode();
		m_Node->attachObject(m_Entity);

		Action(0);
}

Fish::~Fish(void)
{
}

void Fish::SetPosition( Vector3& posn )
{
		m_Position = posn;
		m_Node->setPosition(m_Position);
}

void Fish::SwimTo(std::vector<Vector3> points, int action/*=1*/ )
{
		AnimationData animation;
		animation.m_Action = action;
		for (unsigned int i=0; i<points.size();i++)
		{
				if (i==0)
				{
						double distance = m_Position.distance(points[i]);
						animation.m_EndTime = distance / m_Info->m_Speed;
						animation.m_Path.AddPoint((float)animation.m_EndTime,points[i]);
				}else{
						double distance = points[i-1].distance(points[i]);
						animation.m_EndTime += distance / m_Info->m_Speed;
						animation.m_Path.AddPoint((float)animation.m_EndTime,points[i]);
				}
		}
}
void Fish::AddAnimation( AnimationData& animation )
{
		m_AnimationDatas.push(animation);
}

void Fish::Update( float dt )
{
		if(!m_AnimationDatas.empty())
		{
				m_AnimationDatas.front().m_Time += dt;
				m_Position = m_AnimationDatas.front().m_Path.GetValue((float)m_AnimationDatas.front().m_Time);
				m_Node->setPosition(m_Position);
				if (m_AnimationDatas.front().m_Time >= m_AnimationDatas.front().m_EndTime)
				{
						m_AnimationDatas.pop();
						if (m_AnimationDatas.empty())
						{
								Action(0);
						}else{
								Action(m_AnimationDatas.front().m_Action);
						}
				}
		}
}

void Fish::Action( int index )
{
		AnimationMap::iterator it = m_Info->m_AnimationMap.find(index);
		if (it == m_Info->m_AnimationMap.end())
		{
				m_AnimationsState = m_Entity->getAnimationState(it->second);
		}
		
}
